package com.ttai.presenter.activity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.R;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.FlagBean;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.AddIdAcount;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.LoginId;
import com.ttai.ui.activity.Register;

import java.util.ArrayList;

import retrofit2.Call;

public class AddIdAcountPresenter extends BasePresenter{
    AddIdAcount addIdAcount;
    private ArrayList<String> appList = new ArrayList<>();
    private String phonenumber;
    private String appNumber;
    private String appId;
    public static String flag;

    public AddIdAcountPresenter(AddIdAcount addIdAcount) {
        this.addIdAcount = addIdAcount;
    }

    @Override
    protected void showError(String message) {
//        new AlertDialog.Builder(addIdAcount)
//                .setMessage(message)
//                .setCancelable(true)
//                .setIcon(R.drawable.jieshao)
//                .setTitle("")
//                .setPositiveButton("确定",null)
//                .show();
        addIdAcount.showMessageDialog(message);
    }

    @Override
    protected void parseJson(JsonObject json) {
        if (HomePresrenter.myPhone==null || HomePresrenter.myPhone.isEmpty()){
            if(Register.phonenumber==null || Register.phonenumber.isEmpty()){
                phonenumber = EnterPage.enterphonenumber;
            }else{
                phonenumber =Register.phonenumber;
            }
        }else {
            phonenumber =HomePresrenter.myPhone;
        }
        Gson gson = new Gson();
        FlagBean flagBean = gson.fromJson(json,FlagBean.class);
        flag = flagBean.getFlag();

        Toast.makeText(addIdAcount,"校验成功",Toast.LENGTH_SHORT).show();
        addIdAcount.finish();
        Intent intent=new Intent();
        intent.setClass(addIdAcount,LoginId.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
        addIdAcount.startActivity(intent);
    }

    public ArrayList<String> getListData() {
        DBHelper dbHelper = DBHelper.getInstance(addIdAcount);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.query("appacount", new String[]{"appname"}, null, null,null, null, null);
        while (cursor.moveToNext()){
            String value=cursor.getString(cursor.getColumnIndex("appname"));
            if (value != null){
                appList.add(value);
            }
        }
        cursor.close();
        return appList;
    }
    public void check(String appId,String token,String account,String passWord){
        Call<ResponseInfo> checkInfo = responseInfoApi.addapp(appId, token, account, passWord);
        checkInfo.enqueue(new CallBackAdapter());
    }

    public String getAPPId(String nameNumber) {
        appNumber = nameNumber;
        String appid = null;
        DBHelper dbHelper = DBHelper.getInstance(addIdAcount);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.query("appacount", new String[]{"appid"},"appname= '"+ nameNumber +"'", null,null, null, null);
        while (cursor.moveToNext()){
            appid=cursor.getString(cursor.getColumnIndex("appid"));
        }
        cursor.close();
        appId = appid;
        return appid;
    }
}
